package com.cxx.purchasecharge.core.config;

import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cxx.purchasecharge.core.util.CoreConstants;

public class BaseConfigUtil
{
    private static final Logger LOGGER = Logger.getLogger (BaseConfigUtil.class);

    /**
     * Returns the specified property as a boolean.
     * 
     * @param prop
     * @param name
     * @param def
     * @return boolean
     */
    public static boolean getBooleanProperty (Properties prop, String name, boolean def)
    {
        if (prop == null)
        {
            throw new IllegalArgumentException ("prop cannot be null");
        }

        if (StringUtils.isBlank (name))
        {
            throw new IllegalArgumentException ("name cannot be null or empty string");
        }

        String val = prop.getProperty (name);
        if (val == null || ((!"true".equalsIgnoreCase (val)) && (!"false".equalsIgnoreCase (val))))
        {
            LOGGER.warn ("Unable to parse " + name + ": " + val + " defaulting to " + def);
            return def;
        }

        return Boolean.valueOf (val).booleanValue ();
    }

    /**
     * Returns the specified property as an int
     * 
     * @param prop
     * @param name
     * @param def
     * @return property as int
     */
    public static int getIntProperty (Properties prop, String name, int def)
    {
        if (prop == null)
        {
            throw new IllegalArgumentException ("prop cannot be null");
        }

        if (StringUtils.isBlank (name))
        {
            throw new IllegalArgumentException ("name cannot be null or empty string");
        }

        String val = prop.getProperty (name);
        try
        {
            return Integer.parseInt (val);
        }
        catch (NumberFormatException nfe)
        {
            LOGGER.warn ("Unable to parse " + name + ": " + val + " defaulting to " + def, nfe);
            return def;
        }
    }

    /**
     * Returns the specified property as a long
     * 
     * @param prop
     * @param name
     * @param def
     * @return property as long
     */
    public static long getLongProperty (Properties prop, String name, long def)
    {
        if (prop == null)
        {
            throw new IllegalArgumentException ("prop cannot be null");
        }

        if (StringUtils.isBlank (name))
        {
            throw new IllegalArgumentException ("name cannot be null or empty string");
        }

        String val = prop.getProperty (name);
        try
        {
            return Long.parseLong (val);
        }
        catch (NumberFormatException nfe)
        {
            LOGGER.warn ("Unable to parse " + name + ": " + val + " defaulting to " + def, nfe);
            return def;
        }
    }

    public static String getCxxPropertiesDir ()
    {
        String propDir = System.getProperty (CoreConstants.CXX_PROPS_DIR);
        if (StringUtils.isBlank (propDir))
        {
            String cfxHome = System.getenv (CoreConstants.CXX_HOME_ENV);
            if (StringUtils.isNotBlank (cfxHome))
            {
                return cfxHome + System.getProperty ("file.separator") + CoreConstants.COMMON
                       + System.getProperty ("file.separator") + CoreConstants.PROPERTIES;
            }
        }
        // Just return the propDir
        return propDir;
    }

    public static String getCxxCommonDir ()
    {
        String propDir = System.getProperty (CoreConstants.COMMON_PROPERTY_DIR);
        if (StringUtils.isBlank (propDir))
        {
            String cfxHome = System.getenv (CoreConstants.CXX_HOME_ENV);
            if (StringUtils.isNotBlank (cfxHome))
            {
                return cfxHome + System.getProperty ("file.separator") + CoreConstants.COMMON;
            }
        }
        // Just return the propDir
        return propDir;
    }
}
